*** Generating info for endline tables
* Last updated 20 June by EB ( Corrected attrition tests) 




/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_a05_a06.do 
***********************************

Purpose: 		Generates Appendix Table A5 (reasons for attrition) and A6 (test for differential attrition )

Inputs: 		spes_data.dta
Outputs: 		log_table_a05_a06.log (table generated manually)

Log: 			results/03_output/log_table_a05_a06.log		

Last updated: 	18 November 2021
*/



cap log close 
log using "$output/log_table_a05_a06.log",replace

loc filename "beamquimbo_tables"

use "$usedata_analysis/spes_data",clear 

local n: word count $cov1
	assert `n' == 18 


**************************
*	 Appendix Table A5 
*	Reasons for endline attrition
**************************
  


tab _eeo_attrition_why,sort
// Generate aggregate categories 
gen _eeo_attrition_recode = . 


* 1 Respondent could not be reached
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 2 	// Not correct respondent
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 5 	// Answered, wrong number
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 7 	// No answer, call was busy
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 8 	// No answer, call was not answered
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 12 	// Incorrect number
	replace	_eeo_attrition_recode = 1 if _eeo_attrition_why == 16 	// All contacts exhausted

* 2 Partial interview, no longer reachable

	replace	_eeo_attrition_recode = 2 if  _eeo_attrition_why == 4	// Interviewed partially


* 3 Refused/hung up
	replace	_eeo_attrition_recode = 3 if _eeo_attrition_why == 6 	// Answered, hung up
	replace	_eeo_attrition_recode = 3 if _eeo_attrition_why == 9 	// Refused interview


* 4 Interview scheduled, never recontacted
	replace	_eeo_attrition_recode = 4 if _eeo_attrition_why == 15 	// Refused interview

tab _eeo_attrition_recode

	assert !missing(_eeo_attrition_recode) 	if 	!missing(_eeo_attrition_why)
	assert !missing(_eeo_attrition_recode) 	if  endline == 0
	assert missing(_eeo_attrition_recode) 	if 	missing(_eeo_attrition_why)

lab def attrit_recode 1 "Respondent could not be reached"	2 "Partial interview"	3 "Refused/hung up" 	4 "Scheduled, could not recontact"
lab val _eeo_attrition_recode attrit_recode


// Output results
putexcel set "$tables_analysis/`filename'.xlsx",  sheet(Table A5) modify


tab  _eeo_attrition_recode if endline == 0 , matcell(cellcounts)
	
	decode _eeo_attrition_recode,gen(_attrit_recode)
	levelsof _attrit_recode, local(r_labels)

matrix rownames cellcounts = `r_labels'

putexcel A1 = matrix(cellcounts), names


 
 
**************************
*	 Appendix Table A6 
*	Differential endline attrition 
**************************

* Restrict to study participants
	keep if !missing(treatment) 
	
** Set up matrix
local row ""Baseline respondents" "Attempted to contact" "Response rate" "No covariates" "Covariates""
local col ""Overall" " Not assigned SPES" "Assigned SPES" "P value" "
	matrix P = J(5,4,.)
	matrix rowname P = `row'
	matrix colname P = `col'

local restrictions ""if baseline==1" "if treatment==0" "if treatment==1""

* Baseline respondents
local i = 1
foreach res in `restrictions' {
	qui count `res'
	matrix P[1,`i']=r(N)
	qui count `res' & endline_contacted==1
	matrix P[2,`i']=r(N)
	qui sum endline `res'
	matrix P[3,`i']=r(mean)
	local ++i
}

matrix P[1,4]=.


* No covariates
qui areg endline treatment,absorb(scel) robust
qui test treatment == 0 
matrix P[4,4]=r(p)

*With covariate adjustment
qui areg endline treatment $cov1 ,absorb(scel) robust
qui test treatment == 0 
matrix P[5,4]=r(p)

* Export to Excel
putexcel set "$tables_analysis/`filename'.xlsx", sheet(Table A6) modify
putexcel A1=matrix (P), names nformat(##.##) 


cap log close 

 exit


